package com.zbcz.crm

import android.os.Bundle
import androidx.activity.viewModels
import com.zbcz.crm.bean.AccessoriesRelated
import com.zbcz.crm.databinding.ActivityRelatedAccessoriesBinding
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.viewmodel.RelatedAccessoriesViewModel

class RelatedAccessoriesActivity : BaseActivity() {

    companion object {
        const val PRODUCT_ID = "product_id"
    }

    private lateinit var mBinding: ActivityRelatedAccessoriesBinding

    private val mViewModel: RelatedAccessoriesViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityRelatedAccessoriesBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        init()
    }

    fun init() {
        mBinding.tvTitle.text = "配件详情"
        mBinding.ivBack.onSingleClick {
            finish()
        }
        intent?.run {
            val productId = intent?.getStringExtra(PRODUCT_ID)
            mViewModel.getProductList(productId)
        }
        mViewModel.mResult.observe(this) {
            if (!it.isNullOrEmpty()) {
                setView(it[0])
            }
        }
    }

    private fun setView(accessories: AccessoriesRelated) {
        mBinding.inContent.run {
            tvAccessoriesNameValue.text = accessories.accessoryName
            tvAccessoriesModelValue.text = accessories.accessoryCode
            tvAccessoriesTypeValue.text = accessories.accessoryTypeDictText
            tvAccessoriesPurchaseValue.text = accessories.purchasePrice.toString()
            tvAccessoriesGroupPriceValue.text = accessories.groupPrice.toString()
            tvAccessoriesSellPriceValue.text = accessories.sellingPrice.toString()
        }
    }
}